package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.cdc.busi.dao.CdcLocationMapper;
import com.dhcc.cdc.busi.dao.CdcUserMapper;
import com.dhcc.cdc.busi.entity.CdcLocationEntity;
import com.dhcc.cdc.busi.entity.CdcUserEntity;
import com.dhcc.cdc.busi.service.ICdcSysUerService;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.dao.UserMapper;
import com.dhcc.core.modules.system.entity.User;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
@Service("cdcSysUerService")
public class CdcSysUserServiceImpl implements ICdcSysUerService {
	
	@Autowired
	private CdcUserMapper cdcUserMapper;
	
	@Autowired
	private CdcDataMapper cdcDataMapper;
	
	@Autowired
	private CdcLocationMapper cdcLocationMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	
	@Override
	public String handleXmlData(String dataStr) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Document doc = null;
        try {
            doc = DocumentHelper.parseText(dataStr); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            Iterator<Element> iter = rootElt.elementIterator("user"); // 获取根节点下的子节点user
            // 遍历user节点
            while (iter.hasNext()) {
                Element userEle = iter.next();
                String xcode= userEle.elementTextTrim("xcode"); //唯一标识
                String code= userEle.elementTextTrim("code"); //代码	
                String name= userEle.elementTextTrim("name"); //姓名
                String gender= userEle.elementTextTrim("gender"); //性别
                String telephone= userEle.elementTextTrim("telephone"); //电话	
                String email= userEle.elementTextTrim("email"); //邮箱
                String departmentid= userEle.elementTextTrim("departmentid"); //登录部门
                String createtimestr= userEle.elementTextTrim("createtime"); //创建时间
                String updatetimestr= userEle.elementTextTrim("updatetime"); //更新时间
                String updateuserid= userEle.elementTextTrim("updateuserid"); //更新人
                String opercode= userEle.elementTextTrim("opercode"); //操作码
                User user = new User();
                user.setXcode(xcode);
                //获取dataid
    			CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
    			cdcDataEntityBase.setCode("HisBase");
    			cdcDataEntityBase.setIsActive(1);
    			cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
        		Long basedataId = cdcDataEntityBase.getId();
                if(CommonUtil.isNotEmpty(updatetimestr)) {
            		Date updatetime = sdf.parse(updatetimestr);
            		user.setUpdateDate(updatetime);
            	}
            	//获取更新人
            	if(CommonUtil.isNotEmpty(updateuserid)) {
            		CdcUserEntity cdcUserEntity = new CdcUserEntity();
            		cdcUserEntity.setXcode(updateuserid);
            		cdcUserEntity.setDataId(basedataId);
            		cdcUserEntity.setIsActive(1);
            		cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
            		//判断人员存在不存在
            		Long updateuser = null;
            		if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
            			cdcUserEntity = new CdcUserEntity();
            			cdcUserEntity.setXcode(updateuserid);
                		cdcUserEntity.setIsActive(1);
            			cdcUserEntity.setCode("newuser"+updateuserid);
            			cdcUserEntity.setName("newuser"+updateuserid);
                		cdcUserEntity.setDataId(basedataId);
                		//保存用户
                		cdcUserMapper.insert(cdcUserEntity);
                		updateuser = cdcUserEntity.getId();
            		}else {
            			updateuser = cdcUserEntity.getId();
            		}
            		user.setUpdateBy(updateuser);
            	}
                if("1".equals(opercode)) {//保存/修改
                	if(CommonUtil.isNotEmpty(code)) {
                		user.setUserCode(code);
                	}
                	if(CommonUtil.isNotEmpty(name)) {
                		user.setName(name);
                	}
                	if(CommonUtil.isNotEmpty(gender)) {
						if (gender.equals("男")) {
							user.setSex(1);
						} else {
							user.setSex(2);
						}
                	}
                	if(CommonUtil.isNotEmpty(telephone)) {
                		user.setPhone(telephone);
                	}
                	if(CommonUtil.isNotEmpty(email)) {
                		user.setEmail(email);
                	}
                	if(CommonUtil.isNotEmpty(departmentid)) {
                		CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                		cdcLocationEntity.setXcode(departmentid);
                		cdcLocationEntity.setDataId(basedataId);
                		cdcLocationEntity.setIsActive(1);
                		cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
            			if(CommonUtil.isNotEmpty(cdcLocationEntity)) {//存在
            				Long loclongid = cdcLocationEntity.getId();
            				user.setDeptId(loclongid);
            			}
                	}
                	if(CommonUtil.isNotEmpty(createtimestr)) {
                		Date createtime = sdf.parse(createtimestr);
                		user.setCreateDate(createtime);
                	}
                	user.setStatus(1);
                }else {
                	user.setStatus(3);
                }
                //先判断是不是有该对象
                Wrapper<User> wrapper =  new EntityWrapper<User>();
                wrapper.eq("xcode", user.getXcode());
                int count = userMapper.selectCount(wrapper);
                if(count>0) {
                	userMapper.update(user, wrapper);
                }else {
                	userMapper.insert(user);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        } catch (Exception e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        }
		return "<response><status>1</status><message>ok</message></response>";
	}

}
